12
Easy2Siksha
• For example, numbers[1:3] would give [20, 30], including elements at indices 1 and 2.
Dierent Operators Used on Lists:
Operators are symbols or keywords that perform specic operaons on data. Here are some
commonly used operators when working with lists:
1. Concatenaon Operator (+):
• The concatenaon operator combines two or more lists into a single list.
• For instance, if you have list1 = [1, 2, 3] and list2 = [4, 5, 6], using list1 + list2 results in
a new list [1, 2, 3, 4, 5, 6].
2. Repeon Operator (*):
• The repeon operator duplicates a list by a specied number of mes.
• If list1 = [1, 2, 3], then list1 * 2 yields [1, 2, 3, 1, 2, 3].
3. Membership Operators (in, not in):
• These operators check whether an element is present or absent in a list.
• For example, 2 in list1 returns True as 2 is present in list1, while 4 not in list1 returns
True as 4 is not in list1.
4. Identy Operators (is, is not):
• These operators compare the memory locaon of two objects.
• For instance, list1 is list2 returns False as they are disnct lists, while list1 is not list2
returns True.
5. Assignment Operator (=):
• The assignment operator is used to assign values to variables or modify exisng
elements in a list.
• If list1[0] = 5, it changes the rst element of list1 to 5.
6. Relaonal Operators (<, >, <=, >=, ==, !=):
• These operators compare two lists or elements within lists.
• For example, list1 == list2 checks if the two lists are equal, while list1 < list2 compares
their sizes.
7. Logical Operators (and, or, not):
• Logical operators combine mulple condions.
• If you have two lists, list1 and list2, then len(list1) > 2 and len(list2) < 5 checks if list1
has more than two elements and list2 has fewer than ve elements.
Common List Operaons:
Besides operators, various operaons are commonly performed on lists:
1. Appending Elements: